<?php
require('functions/page_load.php');

$content_nav = true;

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	// insert address
	
	$url = clean_file_name($_POST['company_name']);
	
		$address = $_POST['line_1'].",".$_POST['line_2'].",".$_POST['line_3'].",".$_POST['postcode'];
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=csv&oe=utf8&sensor=false&key=".$settings['google_api_key']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
		$outputArr = explode(",",$output);
		$latitude = $outputArr[2];
		$longitude = $outputArr[3];
	
	$sql = "INSERT INTO directory SET 
			company_name = '".mysql_real_escape_string($_POST['company_name'])."',
			url = '".mysql_real_escape_string($url)."',
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
			line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
			line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
			postcode = '".mysql_real_escape_string($_POST['postcode'])."',
			latitude = '".$latitude."',
			longitude = '".$longitude."',
			telephone = '".mysql_real_escape_string($_POST['telephone'])."',
			website = '".mysql_real_escape_string($_POST['website'])."',
			email = '".mysql_real_escape_string($_POST['email'])."',
			linkback_address = '".mysql_real_escape_string($_POST['linkback_address'])."',
			description = '".mysql_real_escape_string($_POST['description'])."',
			nolinkback = '".mysql_real_escape_string($_POST['nolinkback'])."',
			approved = 1";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_id = mysql_insert_id();
	
	header("Location: edit_directory.php?new=true&id=".$new_id);

}

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="content.php">Pages</a></li>
<li><a href="blog.php">Blog</a></li>
<li><a href="directory.php">Directory</a></li>
<li><a href="faqs.php">FAQs</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Add Company</h2>
	
	<div id="top_links">
	<a href="directory.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Directory</a>
	</div>
	
	<?php echo $results; ?>
	
			<form method="post" action="">
			
			<!-- user details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="company_name">Company name:</label></div>
			<div class="form_row_right"><input type="text" name="company_name" id="company_name" class="textbox" value="<?php echo $rs['company_name']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="description">Description:</label></div>
			<div class="form_row_right"><textarea name="description" id="description" rows="6" cols="30" class="textbox" style="width: 520px; height: 200px;"><?php echo $rs['description']; ?></textarea></div>
			</div>
			
			<!-- address details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="line_1">Address:</label></div>
			<div class="form_row_right">
			<input type="text" name="line_1" id="line_1" class="textbox" value="" />
			<input type="text" name="line_2" id="line_2" class="textbox" value="" />
			<input type="text" name="line_3" id="line_3" class="textbox" value="" />
			<select name="area_id" id="area_id" style="margin-top: 5px;" class="textbox clearLeft">
			<?php
			
			$sql = "SELECT id, area FROM areas ORDER BY area";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($area_rs = mysql_fetch_assoc($area_query)){
			
			echo '<option value="'.$area_rs['id'].'"'.$selected.'>'.$area_rs['area'].'</option>';
			
			}
			
			?></select>
			<input type="text" name="postcode" id="postcode" class="textbox clearLeft" value="<?php echo $rs['postcode']; ?>" style="width: 80px;" />
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="website">Website:</label></div>
			<div class="form_row_right"><input type="text" name="website" id="website" class="textbox" value="<?php echo $rs['website']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="telephone">Telephone:</label></div>
			<div class="form_row_right"><input type="text" name="telephone" id="telephone" class="textbox" value="<?php echo $rs['telephone']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="email">Email:</label></div>
			<div class="form_row_right"><input type="text" name="email" id="email" class="textbox" value="<?php echo $rs['email']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="linkback_address">Linkback address:</label></div>
			<div class="form_row_right"><input type="text" name="linkback_address" id="linkback_address" class="textbox" value="<?php echo $rs['website']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="nolinkback">No Linkback:</label></div>
			<div class="form_row_right"><input type="checkbox" name="nolinkback" id="nolinkback" value="1"<?php echo ($rs['nolinkback'] == '1') ? ' checked="checked"' : '' ; ?> /><span style="margin-left: 10px;">Tick this to override the linkback requirements.</span></div>
			</div>

			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
		
	</div>

</div>
	
<?php
	require('includes/footer.php');
?>